package lium.lbs.lbsim.jgchat.activity.fragment;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.UserInfo;
import com.buz.hjcdriver.bean.GetUserInfoResultBean;
import com.lmlibrary.bean.ResponseBean;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Response;
import lium.lbs.lbsim.App;
import lium.lbs.lbsim.R;
import lium.lbs.lbsim.activity.LoginActivity;
import lium.lbs.lbsim.jgchat.controller.MyeController;
import lium.lbs.lbsim.jgchat.utils.SharePreferenceManager;
import lium.lbs.lbsim.jgchat.utils.ToastUtil;
import lium.lbs.lbsim.jgchat.view.MyeView;
import lium.lbs.lbsim.utils.callbck.JsonCallback;

import java.util.HashMap;

/**
 * Created by ${chenyn} on 2017/2/20.
 */

public class MyeFragment extends BaseFragment {
    private View mRootView;
    public MyeView mMeView;
    private MyeController mMeController;
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getActivity();
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        mRootView = layoutInflater.inflate(R.layout.fragment_my,
                (ViewGroup) getActivity().findViewById(R.id.main_view), false);
        mMeView = (MyeView) mRootView.findViewById(R.id.me_view);
        mMeView.initModule(mDensity, mWidth);
        mMeController = new MyeController(this, mWidth);
        mMeView.setListener(mMeController);
        getData();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup p = (ViewGroup) mRootView.getParent();
        if (p != null) {
            p.removeAllViewsInLayout();
        }
        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void cancelNotification() {
        NotificationManager manager = (NotificationManager) this.getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancelAll();
    }

    //退出登录
    public void Logout() {
        final Intent intent = new Intent();
        UserInfo info = JMessageClient.getMyInfo();
        if (null != info) {
            SharePreferenceManager.setCachedUsername(info.getUserName());
            if (info.getAvatarFile() != null) {
                SharePreferenceManager.setCachedAvatarPath(info.getAvatarFile().getAbsolutePath());
            }
            JMessageClient.logout();
            intent.setClass(mContext, LoginActivity.class);
            startActivity(intent);
        } else {
            ToastUtil.shortToast(mContext, "退出失败");
        }

    }

    public void getData() {
        String memberId = JMessageClient.getMyInfo().getUserName();
        HashMap<String, String> parrmt = new HashMap<String, String>();
        parrmt.put("memberId", memberId);
        postData("/member/getMemberInfo", parrmt, new JsonCallback<ResponseBean<GetUserInfoResultBean>>(getBaseActivity()) {
            @Override
            public void onSuccess(Response<ResponseBean<GetUserInfoResultBean>> response) {
                setData(response.body().data);
            }
        });

    }

    private void setData(GetUserInfoResultBean gRBean) {
        displayImage(gRBean.getHeadImage(), R.drawable.icon_defalut_head, mMeView.headImage);
        mMeView.nickName.setText(gRBean.getNickName());
        mMeView.uniqueCode.setText(gRBean.getUniqueCode());
    }

}
